-
Notifications
You must be signed in to change notification settings - Fork 3.9k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
keys: resolve subtle non-bug by exporting RaftLogKeyFromPrefix #82479
Merged
craig
merged 3 commits into
cockroachdb:master
from
nvanbenschoten:nvanbenschoten/commentSubtle
Jun 7, 2022
Merged
keys: resolve subtle non-bug by exporting RaftLogKeyFromPrefix #82479
craig
merged 3 commits into
cockroachdb:master
from
nvanbenschoten:nvanbenschoten/commentSubtle
Jun 7, 2022
Conversation
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This commit resolves a subtle interaction that came tantalizingly close to a bug in `StateLoader.LoadLastIndex`. The method uses the `StateLoader`'s underlying `keys.RangeIDPrefixBuf` to generate two keys (`RaftLogPrefix` and `RaftLogKey`) that are in use as the same time. `RangeIDPrefixBuf` avoids heap allocations by sharing a single byte slice across all keys that it generates. This is why the type has the comment: > The generated keys are only valid until the next call to one of the > key generation methods. As would be expected, given this comment, the second use of the `RangeIDPrefixBuf` overwrote the buffer and invalidated the first key. However, it happened to get lucky and generate a new key with the same prefix as the old key. As a result, the contents of the first key did not change. To make this aliasing more explicit and avoid this becoming a real bug in the future, we introduce a new `RaftLogKeyFromPrefix` function that callers can use to generate raft log entry keys from a raft log prefix.
…gKey This commit uses the new `RaftLogKeyFromPrefix` function to avoid some redundant encoding work elsewhere due to repeat calls to `RaftLogKey`. Instead of repeatedly encoding the Raft log prefix, we encoded it once and repeatedly rewrite the suffix.
This did not belong in stateloader.go.
nvanbenschoten
force-pushed
the
nvanbenschoten/commentSubtle
branch
from
June 6, 2022 19:17
369e95d
to
c5497ba
Compare
tbg
approved these changes
Jun 7, 2022
erikgrinaker
approved these changes
Jun 7, 2022
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nice catch, thanks!
TFTRs! bors r=tbg,erikgrinaker |
Build succeeded: |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
This commit resolves a subtle interaction that came tantalizingly close to a bug in
StateLoader.LoadLastIndex
. The method uses theStateLoader
's underlyingkeys.RangeIDPrefixBuf
to generate two keys (RaftLogPrefix
andRaftLogKey
) that are in use as the same time.RangeIDPrefixBuf
avoids heap allocations by sharing a single byte slice across all keys that it generates. This is why the type has the comment:As would be expected, given this comment, the second use of the
RangeIDPrefixBuf
overwrote the buffer and invalidated the first key. However, it happened to get lucky and generate a new key with the same prefix as the old key. As a result, the contents of the first key did not change.To make this aliasing more explicit and avoid this becoming a real bug in the future, we introduce a new
RaftLogKeyFromPrefix
function that callers can use to generate raft log entry keys from a raft log prefix.We then use this new function to avoid some redundant encoding work elsewhere due to repeated calls to
RaftLogKey
.